/**
 * Created by zhanghe on 16/4/22.
 */
cur_result = {
    "appUrl": {},
    "webUrl": {},
    "contestCategorys": [
        {
            "id": "",
            "name": "",
            "infoUrl": ""
        }, {
            "id": "",
            "contestId": "",
            "name": "",
            "infoUrl": ""
        }, {
            "id": "",
            "contestId": "",
            "name": "",
            "infoUrl": ""
        }, {
            "id": "",
            "contestId": "",
            "name": "",
            "infoUrl": ""
        }, {
            "id": "",
            "contestId": "",
            "name": "",
            "infoUrl": ""
        }
    ],
    "hostUnit": "",
    "appDetailUrl": {},
    "state": 0,
    "id": "",
    "title": "",
    "refer": "",
    "curStepId": "",
    "prizes": [
        {
            "id": "",
            "title": "",
            "contestCategoryId": "",
            "level": 1,
            "count": 0,
            "contestId": "",
            "refer": "",
            "createdTime": 0,
            "description": "",
            "name": "",
            "url": ""
        }
    ],
    "webDetailUrl": {},
    "tags": "",
    "contestSteps": [
        {
            "submit": false,
            "contestId": "",
            "showPosition": 1,
            "showCount": 1,
            "endTime": 0,
            "quit": false,
            "shows": false,
            "startTime": 0,
            "id": "",
            "showLabel": "",
            "name": "",
            "countDownTips": "",
            "tips": "",
            "showStyle": 0,
            "defaultPage": 0
        }, {
            "submit": false,
            "contestId": "",
            "showPosition": 1,
            "showCount": 1,
            "endTime": 0,
            "quit": false,
            "shows": false,
            "startTime": 0,
            "id": "",
            "showLabel": "",
            "name": "",
            "countDownTips": "",
            "tips": "",
            "showStyle": 0,
            "defaultPage": 0
        }, {
            "submit": false,
            "contestId": "",
            "showPosition": 1,
            "showCount": 1,
            "endTime": 0,
            "quit": false,
            "shows": false,
            "startTime": 0,
            "id": "",
            "showLabel": "",
            "name": "",
            "countDownTips": "",
            "tips": "",
            "showStyle": 0,
            "defaultPage": 0
        }
    ],
    "createdTime": 0,
    "contestStepInfo": {},
    "recommendation": 0,
    "contestPropertyInfo": {
        "sameTypeCount": 0,
        "id": "",
        "hiddenAll": false,
        "stepCount": 0,
        "hiddenRating": false,
        "createdTime": 0,
        "otherDescr": "",
        "categoryCount": 0,
        "infoCollectUrl": "",
        "prizeCount": 0,
        "intro": "",
        "infoCollect": false
    },
    "background": "",
    "contestType": 0
}


navbarApp.controller('prize_contest_controller', function ($scope, $http) {

    $scope.init = function (contestId) {
        var contestId = $.getUrlParam('contestId');
        if(!contestId){
            $scope.result = cur_result;
            return false;
        }
        $.ajax({
            // 提交数据的类型 POST GET
            type: "post",
            // 提交的网址
            url: '/contests/contest/detail',
            // 提交的数据
            data: {
                contestId: contestId
            },
            dataType: "json",// "xml", "html", "script", "json",
            success: function (data) {
                if (data.status == '200') {
                    var cur_contest = data;

                    $scope.$apply(function(){
                        if (typeof(cur_contest) != 'undefined') {
                            if (cur_contest instanceof Array && cur_contest.length) {
                                $scope.result = cur_contest[0];
                            } else {
                                $scope.result = cur_contest.data;
                            }
                            if (!$scope.result.prizes.length){

                                $scope.result.prizes = cur_result.prizes;
                            }
                        }
                    });
                    console.log($scope.result.prizes)
                    console.log($scope.result.contestCategorys)
                    console.log($scope.result)

                } else if (data.status == '302') {
                    layer.msg('您没有该权限');
                } else {
                    layer.msg('服务器错误', {
                        icon: 1,
                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    }, function () {
                        //do something

                    });
                }
            },
            error: function (e) {
            }
        });


    }

    $scope.add_prize = function () {
        $scope.result.prizes.push(
            {
                "id": "",
                "title": "",
                "contestCategoryId": "",
                "level": 0,
                "count": 0,
                "contestId": "",
                "refer": "",
                "createdTime": 1461051156419,
                "description": "",
                "name": "",
                "url": ""
            }
        );
    }
    $scope.remove_prize = function (index) {
        $scope.result.prizes.splice(index, 1);
    }


    //提交配置保存
    $scope.btn_submit = function () {
        var contestId = $.getUrlParam('contestId');
        var dd = angular.copy($scope.result);
        if (typeof(dd.state) == 'boolean') {
            if (dd.state) {
                dd.state = -10;
            } else {
                dd.state = 0;
            }
        }

        //去掉分类中为空的数据
        for (var a in dd.contestCategorys) {
            if (!dd.contestCategorys[a].name) {
                dd.contestCategorys.splice(a, 1);
            }
        }
        for (var a in dd.contestSteps) {
            if (!dd.contestSteps[a].name) {
                dd.contestSteps.splice(a, 1);
            }
        }

        console.log(dd);


        layer.confirm('确定保存?', {icon: 3, title: '提示'}, function (index) {
            //do something
            $.ajax({
                // 提交数据的类型 POST GET
                type: "post",
                // 提交的网址
                url: '/contests/contest/addPrizes',
                // 提交的数据
                data: {
                    jsonData: sjApp.stringify(dd.prizes),
                    contestId:contestId
                },
                dataType: "json",// "xml", "html", "script", "json",
                success: function (data) {
                    if (data.status == '200') {
                        layer.msg('保存成功', {
                            icon: 1,
                            time: 2000 //2秒关闭（如果不配置，默认是3秒）
                        }, function () {

                            //do something
                            // $("#dataTable").dataTable().fnDraw();
                        });
                    } else if (data.status == '302') {
                        layer.msg('您没有该权限');
                    } else {
                        layer.msg('服务器错误', {
                            icon: 1,
                            time: 2000 //2秒关闭（如果不配置，默认是3秒）
                        }, function () {
                            //do something

                        });

                    }

                },
                error: function (e) {
                }
            });

            layer.close(index);
        });
    }




$scope.init();


});

